<?php

class KiritoAction extends Action{
    function index(){
        $User = D('Login');
        $input=$User->create();
        if(empty($input)){
            $this->ajaxReturn(array('status'=>false,'message'=>$User->getError()));
            return;
        }
        $user=$User->getByName($input['name']);
        if(empty($user)){
            $this->ajaxReturn(array('status'=>false,'message'=>'用户不存在'));
            return;
        }
        if($user['password']!=$input['password']){
            $this->ajaxReturn(array('status'=>false,'message'=>'密码错了'));
            return;
        }
        session('userId',$user['id']);
        $this->ajaxReturn(array('status'=>true));
    }

    function out(){
        session(null);
    }

    function registerButton(){
        $a=M("Login");
        $input=$a->create();
        if($input["name"]==null||$input["password"]==null){
            $this->ajaxReturn(array('mss'=>"信息不完整"));
            return;
        }else{
            $data = $a->where("name=%s",$input['name'])->select();
            if(!empty($data)){
                $this->ajaxReturn(array('mss'=>"该账号已被注册"));
                return;
            }else{
                $a->add(
                    array(
                        "name"=>$input['name'],
                        "password"=>$input['password'],
                        "money"=>0
                    )
                );
                $this->ajaxReturn(array('mss'=>"注册成功"));
            }
        }
    }
}